'use strict';

/* Services */


// Demonstrate how to register services
// In this case it is a simple value service.
angular.module('myApp.services', []).
  value('version', '0.1');

var myApp = angular.module('myApp.services',[]);
myApp.service('ConversionServices', function() {
	
	this.convertJSONStringToTable = function(jsonString) {
		var table = [];
		var nextComma = 0;
		while ((nextComma=(jsonString.indexOf("}")+1)) > 0) {
			table.push(jsonString.substring(0,nextComma));
			jsonString = jsonString.substring(nextComma+1,jsonString.length);
		}
		return table;
	};
});


myApp.factory('PatientServices', function($http) {

	var global_patient = {};
	if (sessionStorage && sessionStorage.getItem('GLOBAL_PATIENT'))
		global_patient.data = JSON.parse(sessionStorage.getItem('GLOBAL_PATIENT'));
		
	global_patient.comparePatients = function(patient1,patient2) {
		if (!patient1 || !patient2) return false;
		return 	patient1.nom == patient2.nom && 
				patient1.prenom == patient2.prenom && 
				patient1.id == patient2.id;
	};

	global_patient.hasGlobalPatient = function() {
		if (!sessionStorage || !global_patient) return false;
		return (global_patient.data=JSON.parse(sessionStorage.getItem('GLOBAL_PATIENT')))?true:false;
	};

	global_patient.getGlobalPatientString = function() {
		if (global_patient.hasGlobalPatient())
			return ((global_patient.data.sexe=='F')?"Madame ":"Monsieur ")+global_patient.data.nom +" " + global_patient.data.prenom +" [" + global_patient.data.date_naissance + "]";
		return "";
	}

	global_patient.getGlobalPatient = function() {
		if (global_patient.hasGlobalPatient()) return global_patient.data;
		return null;
	};
   
	global_patient.setGlobalPatient = function(patient) {
		// This patient should not be saved
		if (!patient.nom && !patient.prenom && !patient.id) return;
		global_patient.data = patient;
		sessionStorage.setItem('GLOBAL_PATIENT',JSON.stringify(patient));
	};

	global_patient.getGlobalName = function() {
		if (global_patient.hasGlobalPatient())
			return global_patient.data.nom;
		return "";
	};

	global_patient.getGlobalSurname = function() {
		if (global_patient.hasGlobalPatient())
			return global_patient.data.prenom;
		return "";
	};
	
	global_patient.getAll = function(array,callback) {
		$http.post('/api/patient/all',{data: array}).
			success(function(data, status, headers, config) {
				if(callback) callback(data,status);
			}).
			error(function(data, status, headers, config) {
				if(callback) callback(data,status);
			});
	};
		
	global_patient.readData = function(patient,callback) {
		$http.post('/api/patient/get/',patient).
			success(function(data, status, headers, config) {
				global_patient.setGlobalPatient(data);
				if(callback) callback(data,status);
			}).
			error(function(data, status, headers, config) {
				if(callback) callback(data,status);
			});
	};
		
	global_patient.saveData = function(patient,callback) {
		var dataTMP = global_patient.data;
		if(patient) dataTMP = patient;

		$http.post('/api/patient/update/',dataTMP).
			success(function(data, status, headers, config) {
				global_patient.setGlobalPatient(data);
				if(callback) callback(status);
			}).
			error(function(data, status, headers, config) {
				if(callback) callback(status);
			});
	};

	global_patient.deleteData = function(patient,callback) {
		var dataTMP = global_patient.data;
		if(patient) dataTMP = patient;

		$http.post('/api/patient/delete/',dataTMP).
			success(function(data, status, headers, config) {
				if(callback) callback(status);
			}).
			error(function(data, status, headers, config) {
				if(callback) callback(status);
			});
	};
	return global_patient;
   
});

myApp.factory('WaitingList', function(ConversionServices,PatientServices) {
	var waiting_list = {};
	waiting_list.data=[];
	if(localStorage.getItem('waiting_list'))
		waiting_list.data=ConversionServices.convertJSONStringToTable(localStorage.getItem('waiting_list'));
	
	waiting_list.getDataJSON = function() {
		var list = [];
		waiting_list.data.forEach(function(item) {list.push(JSON.parse(item));});
		return list;
	};
	
	waiting_list.addOrRemoveItem =  function(patient) {
//		alert(JSON.stringify(patient));
		if (patient.here) {
			this.data.push(JSON.stringify(patient));
		}
		else {
			for (var i = 0;	i < this.data.length; i++){
//				alert("parsing Element "+this.data[i]);
				if (PatientServices.comparePatients(JSON.parse(this.data[i]),patient)) {
					this.data.splice(i,1);
	//				alert("element found and removed");
					break;
				}
			}
		}
	//	alert("waiting_list "+waiting_list.length);
		localStorage.setItem('waiting_list',this.data);
	};
	
	return waiting_list;
});


myApp.factory('MedecinServices', function($http) {

	var global_medecin = {};
	if (sessionStorage && sessionStorage.getItem('GLOBAL_MEDECIN'))
		global_medecin.data = JSON.parse(sessionStorage.getItem('GLOBAL_MEDECIN'));
		
	global_medecin.compareMedecins = function(medecin1,medecin2) {
		if (!medecin1 || !medecin2) return false;
		return 	medecin1.nom == medecin2.nom && 
				medecin1.prenom == medecin2.prenom && 
				medecin1.id == medecin2.id;
	};

	global_medecin.hasGlobalMedecin = function() {
		if (!sessionStorage || !global_medecin) return false;
		return (global_medecin.data=JSON.parse(sessionStorage.getItem('GLOBAL_MEDECIN')))?true:false;
	};

	global_medecin.getGlobalMedecinString = function() {
		if (global_medecin.hasGlobalMedecin())
			return ((global_medecin.data.sexe=='F')?"Madame ":"Monsieur ")+global_medecin.data.nom +" " + global_medecin.data.prenom +" [" + global_medecin.data.date_naissance + "]";
		return "";
	}

	global_medecin.getGlobalMedecin = function() {
		if (global_medecin.hasGlobalMedecin()) return global_medecin.data;
		return null;
	};
   
	global_medecin.setGlobalMedecin = function(medecin) {
		// This medecin should not be saved
		if (!medecin.nom && !medecin.prenom && !medecin.id) return;
		global_medecin.data = medecin;
		sessionStorage.setItem('GLOBAL_MEDECIN',JSON.stringify(medecin));
	};

	global_medecin.getGlobalName = function() {
		if (global_medecin.hasGlobalMedecin())
			return global_medecin.data.nom;
		return "";
	};

	global_medecin.getGlobalSurname = function() {
		if (global_medecin.hasGlobalMedecin())
			return global_medecin.data.prenom;
		return "";
	};
	
	global_medecin.getAll = function(array,callback) {
		$http.post('/api/medecin/all',{data: array}).
			success(function(data, status, headers, config) {
				if(callback) callback(data,status);
			}).
			error(function(data, status, headers, config) {
				if(callback) callback(data,status);
			});
	};
		
	global_medecin.readData = function(medecin,callback) {
		$http.post('/api/medecin/get/',medecin).
			success(function(data, status, headers, config) {
				global_medecin.setGlobalMedecin(data);
				if(callback) callback(data,status);
			}).
			error(function(data, status, headers, config) {
				if(callback) callback(data,status);
			});
	};
		
	global_medecin.saveData = function(medecin,callback) {
		var dataTMP = global_medecin.data;
		if(medecin) dataTMP = medecin;

		$http.post('/api/medecin/update/',dataTMP).
			success(function(data, status, headers, config) {
				global_medecin.setGlobalMedecin(data);
				if(callback) callback(status);
			}).
			error(function(data, status, headers, config) {
				if(callback) callback(status);
			});
	};

	global_medecin.deleteData = function(medecin,callback) {
		var dataTMP = global_medecin.data;
		if(medecin) dataTMP = medecin;

		$http.post('/api/medecin/delete/',dataTMP).
			success(function(data, status, headers, config) {
				if(callback) callback(status);
			}).
			error(function(data, status, headers, config) {
				if(callback) callback(status);
			});
	};
	return global_medecin;   
});
